<?php

namespace App\Admin\Controllers;

use App\Admin\Actions\Post\AutoMsgVerfyBad;
use App\Admin\Actions\Post\AutoMsgVerfyOk;
use App\Admin\Extensions\AutoMsgOp;
use App\Http\Controllers\Controller;
use App\Model\Guild\GuildHostManage;
use App\Model\GuildManage;
use App\Model\UserAutoMsg;
use App\Util\ComUtil;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Grid;
use Encore\Admin\Controllers\ModelForm;
use Encore\Admin\Layout\Content;

class AutoMsgController extends Controller
{
    use ModelForm;

    public function index()
    {
        $content = new Content();
        $content->header('打招呼消息审核管理');
        $content->body($this->grid());
        return $content;
    }

    protected function grid()
    {


        if (empty(request()->flag)) {
            request()->merge(['flag' => UserAutoMsg::STATUS_ING]);
        }
        $grid = new Grid(new UserAutoMsg());
        $where = [];

        $adminId = Admin::user()->id;
        if($adminId == 50 || $adminId == 51 ){
            $ghId= GuildManage::getGuidIdByGuildUserId($adminId);

            $userIds = GuildHostManage::whereIn('guild_manage_id',$ghId)->pluck('host_user_id')->toArray();
            $grid->model()->where($where)->whereIn('user_id',$userIds)->orderBy('id', 'desc');
        }else{
            $grid->model()->where($where)->orderBy('id', 'desc');
        }


        $grid->id('记录ID');
        $grid->userProfile()->user_profile_id('用户ID');
        $grid->userProfile()->nick_name('用户昵称');
        $grid->userProfile()->gender('用户性别')->display(function ($gender) {
            return $gender == 1 ? '男' : ($gender == 0 ? '女' : '-');
        });
        $grid->userProfile()->nick_name('用户昵称');
        $grid->userProfile()->avatar('头像')->display(function ($data) {
            if (!$data) return '';
            return ComUtil::buidlImgaeHtml($data);
        });
        $grid->personalProfile()->face_vry_pic('认证实人照片')->display(function ($data) {
            if (!$data) return '-';
            return ComUtil::buidlImgaeHtml($data);
        });
        $grid->avatarVerf()->content('真人照片')->display(function ($data) {
            if (!$data) return '-';
            $data = json_decode($data, true);
            if(empty($data['real_avatar'])) return '';
            return ComUtil::buidlImgaeHtml($data['real_avatar']);
        });
        // $grid->user()->phone('手机号');
        $grid->user()->version('用户当前版本');
        $grid->msg_type('消息类型')->display(function ($data) {
            if (!$data) return '-';
            return UserAutoMsg::TYPE_DESC[$data] ?? '未知类型';
        });
        $grid->msg_content('消息内容')->display(function ($data) {
            if (!$data) return '-';
            switch ($this->msg_type) {
                case 'image':
                    return ComUtil::buidlImgaeHtml($data);
                case 'voice':
                    return ComUtil::buildAudio($data, 'audio');
                case 'video':
                    return ComUtil::buildAudio($data, 'video');
                default:
                    return $data;
            }
        });
        $grid->flag('状态')->display(function ($data) {
            return UserAutoMsg::STATUS[$data] ?? '未知状态';
        });
        $grid->created_at('设置时间')->display(function ($time) {
            return !$time ? '-' : date('Y-m-d H:i:s', strtotime($time));
        });
        $grid->disableExport();
        // $grid->disableRowSelector();
        $grid->filter(function ($filter) {
            $filter->expand();
            $filter->disableIdFilter();
            $filter->column(1 / 2, function ($filter) {
                $filter->equal('userProfile.user_profile_id', '用户ID');
                $filter->equal('flag', '状态')->select(UserAutoMsg::STATUS);
            });
            $filter->column(1 / 2, function ($filter) {
                $filter->like('user.phone', '手机号');
                $filter->equal('msg_type', '消息类型')->select(UserAutoMsg::TYPE_DESC);
            });
        });

        $grid->actions(function (Grid\Displayers\Actions $actions) {
            $actions->disableDelete();
            $actions->disableView();
            $actions->disableEdit();
            $actions->append(new AutoMsgOp(
                $actions->getKey(),
                $actions->row,
                'automsg/verify'
            ));
        });

        $grid->batchActions(function ($batch) {
            $batch->add(new AutoMsgVerfyOk());
            $batch->add(new AutoMsgVerfyBad());
        });
        return $grid;
    }

    function verify()
    {
        $id = request('id');
        $status = request('status');
        $record = UserAutoMsg::find($id);
        if (!$record) return ['ret' => -1, 'message' => '记录不存在'];
        //修改状态
        $record->update(['flag' => $status]);
        //站内通知 - 违规
        if (UserAutoMsg::STATUS_FAIL == $status){
            UserAutoMsg::veryfyBad($record);
        }
        return ['ret' => 0, 'message' => '操作成功！'];
    }
}
